package com.lyc.controller.user;

import com.lyc.pojo.Like;
import com.lyc.pojo.Result;
import com.lyc.service.LkService;
import com.lyc.utils.ThreadLocalUtil;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Validated //  开启参数校验
@CrossOrigin // 添加这一行允许跨域
@RestController
@RequestMapping("/lk")
public class LkController {
    @Autowired
    private LkService lkService;

    //根据userId和postId获取用户对该帖子的所有互动（点赞和收藏）
    @GetMapping("/getLkId")
    public Result<List<Like>> getLkId(@RequestParam Integer userId, @RequestParam Integer postId) {
        List<Like> likes = lkService.getLkId(userId, postId);
        return Result.success(likes);
    }

    //添加点赞
    @PostMapping("/addLike")
    public Result addLike(@RequestBody Like like) {
        return lkService.addLike(like);
    }

    //添加收藏
    @PostMapping("/addFavorite")
    public Result addFavorite(@RequestBody Like like) {
        return lkService.addFavorite(like);
    }
    
    // 获取用户点赞列表
    @GetMapping("/UserLkList")
    public Result<List<Like>> getUserLkList(@RequestParam Integer userId, @RequestParam String type){
        List<Like> lkList = lkService.getUserLkList(userId, type);
        return Result.success(lkList);
    }

    // 获取用户收藏列表
    @GetMapping("/UserFavorites")
    public Result<List<Like>> getUserFavorites(@RequestParam Integer userId, @RequestParam String type){
        List<Like> favorites = lkService.getUserFavorites(userId, type);
        return Result.success(favorites);
    }

    //判断当前用户是否点赞
    @GetMapping("/isLike")
    public Result<Boolean> isLiked(@RequestParam Integer userId,@RequestParam  Integer postId){
        return Result.success(lkService.isLiked(userId, postId));
    }

    //判断当前用户是否收藏
    @GetMapping("/isFavorite")
    public Result<Boolean> isFavorite(@RequestParam Integer userId, @RequestParam Integer postId){
        return Result.success(lkService.isFavorite(userId, postId));
    }

    //获取帖子作者
    @GetMapping("/author/{userId}")
    public Result<String> getAuthor(@PathVariable Integer userId){
        String author = lkService.getAuthor(userId);
        return Result.success(author);
    }

    //获取帖子总点赞
    @GetMapping("/likeCount/{postId}")
    public Result<Integer> getLikeCount(@PathVariable Integer postId){
        Integer likeCount = lkService.getLikeCount(postId);
        return Result.success(likeCount);
    }

    //获取帖子总收藏
    @GetMapping("/favoriteCount/{postId}")
    public Result<Integer> getFavoriteCount(@PathVariable Integer postId){
        Integer favoriteCount = lkService.getFavoriteCount(postId);
        return Result.success(favoriteCount);
    }
    //取消点赞
    @DeleteMapping("/like/{lkId}")
    public Result removeLike(@PathVariable Integer lkId){
        lkService.removeLike(lkId);
        return Result.success();
    }

    // 取消收藏
    @DeleteMapping("/favorite/{lkId}")
    public Result removeFavorite(@PathVariable Integer lkId){
        lkService.removeFavorite(lkId);
        return Result.success();
    }
}